package canglang.monitor.dept.action;

import canglang.monitor.core.base.action.BaseAction;
import canglang.monitor.core.base.pagination.PaginatedListHelper;
import canglang.monitor.core.base.properties.AppDefaultProperty;
import canglang.monitor.core.base.util.DateUtil;
import canglang.monitor.core.base.util.StringUtil;
import canglang.monitor.dept.model.DeptModel;
import canglang.monitor.dept.service.DeptMgr;
import canglang.monitor.dept.to.SearchTO;
import canglang.monitor.user.model.UserModel;
import com.opensymphony.xwork2.util.CreateIfNull;
import com.opensymphony.xwork2.util.Element;

import java.util.Date;
import java.util.List;

public class DeptAction extends BaseAction {

	private static final long serialVersionUID = -7447890730726217846L;

    private DeptModel deptModel;

    private DeptMgr deptMgr;

    private PaginatedListHelper helper;

    private SearchTO searchTO;

    public SearchTO getSearchTO() {
        return searchTO;
    }

    public void setSearchTO(SearchTO searchTO) {
        this.searchTO = searchTO;
    }

    public DeptModel getDeptModel() {
        return deptModel;
    }

    public void setDeptModel(DeptModel deptModel) {
        this.deptModel = deptModel;
    }

    public DeptMgr getDeptMgr() {
        return deptMgr;
    }

    public void setDeptMgr(DeptMgr deptMgr) {
        this.deptMgr = deptMgr;
    }

    public PaginatedListHelper getHelper() {
        return helper;
    }

    public void setHelper(PaginatedListHelper helper) {
        this.helper = helper;
    }

    @Element(value = DeptModel.class)
    @CreateIfNull(value = true)
    public List getResultList() {
        return helper != null ? helper.getList() : null;
    }

    public void setResultList(List alist) {
        if (helper == null)
            helper = new PaginatedListHelper();
        helper.setList(alist);
    }

    public String searchDept() {
        if (searchTO == null) {
            searchTO = new SearchTO();
        }
        if (helper == null) {
            helper = new PaginatedListHelper();
            helper.setSortCriterion("DEPT_NAME");
        }
        helper = deptMgr.search(helper, searchTO);
        return SUCCESS;
    }

    public String deleteDept() {
        for (int i = 0; i < helper.getList().size(); i++) {
            DeptModel u = (DeptModel) helper.getList().get(i);
            if (u != null && !StringUtil.convertEmpty2Null(u.getSelectId()).equalsIgnoreCase("") && !StringUtil.convertEmpty2Null(u.getSelectId()).equalsIgnoreCase("false")) {
                deptMgr.deleteDept(Integer.parseInt(u.getSelectId()));
            }
        }
        this.saveMessage(AppDefaultProperty.getProperty("label.delete.successful"));
        return searchDept();
    }

    public String addDept() {
        String deptName = deptModel.getDeptName();
        DeptModel u = deptMgr.getDept(deptName);
        if (u != null) {
            this.addActionError(AppDefaultProperty.getProperty("label.record.already.exist"));
            return INPUT;
        }
        UserModel user = (UserModel) this.getSession().getAttribute("user");
        deptModel.setCreateId(user.getUserId());
        deptModel.setUpdateId(user.getUserId());
        Date date = DateUtil.getDate();
        deptModel.setCreateDt(date);
        deptModel.setUpdateDt(date);
        deptModel=deptMgr.addDept(deptModel);
        this.saveMessage(AppDefaultProperty.getProperty("label.record.add.successful"));
        return SUCCESS;
    }

    public String viewDept() {
        String selectId = deptModel.getSelectId();
        deptModel= deptMgr.getDept(Integer.parseInt(selectId));
        return SUCCESS;
    }

    public String saveDept() {
        String deptName = deptModel.getDeptName();
        DeptModel u = deptMgr.getDept(deptName);
        if (u != null&&u.getDeptId()!=deptModel.getDeptId()) {
            this.addActionError(AppDefaultProperty.getProperty("label.record.already.exist"));
            return INPUT;
        }
        UserModel user = (UserModel) this.getSession().getAttribute("user");
        deptModel.setUpdateId(user.getUserId());
        Date date = DateUtil.getDate();
        deptModel.setUpdateDt(date);
        deptModel=deptMgr.saveDept(deptModel);
        this.saveMessage(AppDefaultProperty.getProperty("label.record.modify.successful"));
        return SUCCESS;
    }

}
